package com.yoyo.yyadsdk.serv;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.yoyo.ad.main.IAdView;
import com.yoyo.ad.main.YoYoAd;
import com.yoyo.ad.main.YoYoAdManager;
import com.yoyo.yyadsdk.R;
import com.yoyo.yyadsdk.config.PullFeature;
import com.yoyo.yyadsdk.util.DensityUtil;
import com.yoyo.yyadsdk.widget.DragView;

import java.util.List;

import static com.yoyo.yyadsdk.config.Constants.ICON_UP;
import static com.yoyo.yyadsdk.config.Constants.TYPE_MOULD;


public class WindowIUService extends WindowBaseService {

    private DragView mIconIv;

    @Override
    protected int getLayoutId() {
        return R.layout.common_icon;
    }

    @Override
    protected void initStyle(WindowManager.LayoutParams mParams) {
        mParams.gravity = Gravity.TOP | Gravity.START;
        int length = DensityUtil.dp2px(this, 24);
//        mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;//窗口的宽和高
        mParams.width = length;//窗口的宽和高
        mParams.height = length;
        mParams.x = (int) (mWidth * 0.9);//窗口位置的偏移量
        mParams.y = (int) (mHeight * 0.2);
    }

    @Override
    protected int getAdvType() {
        return PullFeature.ADV_ICON_UP;
    }

    @Override
    protected void initInflate(View inflate) {
        mIconIv = inflate.findViewById(R.id.icon_iv);
//        mContainer = inflate.findViewById(R.id.container);
        mIconIv.setWmParams(mParams, mWidth);
    }

    @Override
    protected void initData() {
//        mIconIv.setImageResource(R.mipmap.huo);
//        show();
        mAdFactory = YoYoAdManager.getAdFactory(this, new IAdView() {

            @Override
            public void adSuccess(int requestCode, List<YoYoAd> t) {
                final YoYoAd yoYoAd = t.get(0);
                if (yoYoAd != null) {
//                    Glide.with(mIconIv).load(yoYoAd.getImgUrl1()).into(mIconIv);
                    Glide.with(mIconIv).asBitmap().load(yoYoAd.getImgUrl1()).into(new CustomTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            mParams.width = resource.getWidth();//窗口的宽和高
                            mParams.height = resource.getHeight();
                            mIconIv.setWmParams(mParams, mWidth);
                            mIconIv.setImageBitmap(resource);
                            yoYoAd.onAdClicked(mIconIv, null);
                            yoYoAd.show(null);
                            loadSuccess();
                        }

                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {
                            loadFail();
                            stopSelf();
                        }
                    });
                } else {
                    loadFail();
                    stopSelf();
                }
            }

            @Override
            public void adFail(int requestCode, String arg0) {
                loadFail();
                stopSelf();
                Log.e(TAG, arg0);
            }

            @Override
            public void onAdClick(int requestCode, View view) {
                stopSelf();
            }
        });
    }

    @Override
    public void onAdSuccess() {
        mAdFactory.getYoyoAd(ICON_UP, 1, 1, TYPE_MOULD);
    }
}